<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //    let [a,b,c] = [1,2,3]
        //    console.log(a,b,c);
        let { mayName: a, maAge: b } = { mayName: 'hello', maAge: '20' }
        console.log(a);
        console.log(b);

        //嵌套使用
        let [aa, { x }, bb] = [111, { x: 222 }, 333]
        console.log(aa);
        console.log(x);
        console.log(b);
        //字符串可以使用解构赋值，数字和布尔值不支持
        let [foo, foo2] = 'hello'
        //    let [bar] = 123;
        //    let [baz] = true;
        console.log(foo, foo2);
        //    console.log(bar);
        //    console.log(baz);

        // rest参数 也叫不定参数

        let [j, k, ...l] = [1, 2, 3, 4, 5, 6, 7, 8]
        console.log(j);
        console.log(k);
        console.log(l);

        //函数的形参不包含rest参数
        function fun(a, b, ...c) {
            console.log(c);
            console.log(arguments[0]);
            console.log(fun.length);
        }
        fun(888, 2, 3, 4, 5)

        // 一个函数假设只能传入num，可以不定参数，把所有的参数相加输出结果用ES5和ES6两种写法
        // ES5
        function foot() {
            var result = 0;
            for (var i = 0; i < arguments.length; i++) {
                result += arguments[i];
            }
            console.log("所有实参累加的结果求和是：" + result);
        }
        foot(1, 2, 3);

        // ES6
        function add(...values) {
            let sum = 0;
            for (var val of values) {
                sum += val;
            }
            return sum;
        }
        console.log(add(2, 5, 30));
      

        function fn( ...z) {
            let sum = 0;
           z.forEach(function (item){
               sum += item;
           })
            console.log(sum)
        }
        fn(1, 2, 7, 4, 5)
    </script>
</body>

</html>